package org.example.view;

import org.example.entity.FreeTicket;
import org.example.entity.Movie;
import org.example.entity.StudentTicket;
import org.example.entity.Ticket;
import org.example.util.TicketFactory;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.util.jar.JarEntry;

/**
 * @author TonySong
 * @date 2025/8/6 0006
 * @time 18:27
 */
public class BuyTiaketFrmae extends JFrame {

    private Movie movie;
    private  void init() {
        JLabel labName = new JLabel("电影名称");
        JLabel labDirector =new JLabel("导演");
        JLabel lalActor = new JLabel("主演");
        JLabel labType = new JLabel("电影类型");
        JLabel labPrice = new JLabel("票价");
        JLabel labPlayTime = new JLabel("放映时间");
        JTextField txtName = new JTextField(10);
        JTextField txtDirector = new JTextField(10);
        JTextField txtActor = new JTextField(10);
        JTextField txtType = new JTextField(10);
        JTextField txtPrice = new JTextField(10);
        JTextField txtPlayTime= new JTextField(10);
        //
        txtName.setEditable(false);
        txtDirector.setEditable(false);
        txtActor.setEditable(false);
        txtType.setEditable(false);
        txtPrice.setEditable(false);
        txtPlayTime.setEditable(false);

        txtName.setText(movie.getName());
        txtDirector.setText(movie.getDirection());
        txtActor.setText(movie.getActor());
        txtType.setText(movie.getType().toString());
        txtPrice.setText(movie.getPrice().toString());
        txtPlayTime.setText(movie.getPlayTime().toString());
        JComboBox<String> comboBox = new JComboBox<>(new String[]{"普通票","学生票","赠票"});
        JButton btnBuy =new JButton("购买") ;
        JButton btnCancel=new JButton("取消");
        this.setLayout(new GridLayout(10,2));
        this.add(labName);this.add(txtName);
        this.add(labDirector);this.add(txtDirector);
        this.add(lalActor);this.add(txtActor);
        this.add(labType);this.add(txtType);
        this.add(labPrice);this.add(txtPrice);
        this.add(labPlayTime);this.add(txtPlayTime);
        this.add(comboBox);
        this.add(btnBuy);this.add(btnCancel);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setSize(400,400);
        btnBuy.addActionListener(e->{
            Object object = comboBox.getSelectedItem();
            int discount=0;
            String customerName=null;
            if(object instanceof String){
                String name = (String) object;
                switch (name){
                    case "学生票":
                    String inputDialog = JOptionPane.showInputDialog("请输入折扣：", 8);
                    discount = Integer.parseInt(inputDialog);
                    break;
                    case "赠票":
                         customerName = JOptionPane.showInputDialog("请输入客户姓名");
                        break;
                    default:
                        name="普通票";
                        break;
                }
                Ticket ticket = TicketFactory.createTicket(movie, name);
                if(ticket instanceof StudentTicket){
                    ((StudentTicket) ticket).setDiscount(discount);
                }
                if(ticket instanceof FreeTicket){
                    ((FreeTicket) ticket).setCustomerName(customerName);
                }
                if(ticket!=null){
                    try {
                        ticket.print();
                        JOptionPane.showMessageDialog(null,"购票成功！");
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        });

    }

    public BuyTiaketFrmae(Movie movie){
        this.movie= movie;
        init();
    }
    private BuyTiaketFrmae(){}
    private static BuyTiaketFrmae buyTiaketFrmae;
    public static BuyTiaketFrmae getInstance(Movie movie) {
        if(buyTiaketFrmae==null){
            buyTiaketFrmae=new BuyTiaketFrmae(movie);
        }
        return buyTiaketFrmae;
    }
}
